package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;

import org.json.JSONArray;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.log.Toaster;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.toolbox.JsonUtils;
import com.lv.phuotbacnam.ui.Constants;
import com.lv.phuotbacnam.ui.Section;
import com.lv.phuotbacnam.ui.activity.BackActivity;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class ChiTietDiaDiem extends BaseFragment {

	private ParseObject dia_diem;
	private ParseObject nguoi_tao;

	// Nguoi Dang
	private TextView nguoi_tao_value;
	private TextView diem_tich_luy_value;
	private TextView cap_bac_value;
	private ImageView chi_tiet_bai_dang_avatar;

	// Dia diem
	private TextView ten_dia_diem_value;
	private TextView luot_xem_value;
	private TextView luot_visited_value;
	private TextView diem_binh_chon_value;
	private JSONArray list_image_url;

	// Bai dang && nhan xet
	private LinearLayout bai_dang_list;
	private LinearLayout nhan_xet_list;
	private Button bai_dang_button;
	private Button nhan_xet_button;

	private int[] list_id_image = { R.id.add_image_one, R.id.add_image_two,
			R.id.add_image_three, R.id.add_image_four, R.id.add_image_five,
			R.id.add_image_six };

	private ChiTietDiaDiemSupport diemSupport;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_chi_tiet_dia_diem,
				container, false);
		try {
			dia_diem = (ParseObject) IntentHelper
					.getObjectForKey(Constants.KEY_DIA_DIEM);
			nguoi_tao = dia_diem
					.getParseUser(QueriesConstant.TableDiaDiem.COLUMN_nguoi_tao);
		} catch (Exception e) {
			errorGetBaiDang();
			return null;
		}

		nguoi_tao_value = (TextView) rootView
				.findViewById(R.id.nguoi_tao_value);
		diem_tich_luy_value = (TextView) rootView
				.findViewById(R.id.diem_tich_luy_value);
		cap_bac_value = (TextView) rootView.findViewById(R.id.cap_bac_value);
		chi_tiet_bai_dang_avatar = (ImageView) rootView
				.findViewById(R.id.chi_tiet_bai_dang_avatar);

		ten_dia_diem_value = (TextView) rootView
				.findViewById(R.id.ten_dia_diem);
		luot_xem_value = (TextView) rootView.findViewById(R.id.luot_xem_value);
		luot_visited_value = (TextView) rootView
				.findViewById(R.id.luot_visited_value);
		diem_binh_chon_value = (TextView) rootView
				.findViewById(R.id.diem_binh_chon_value);

		nhan_xet_list = (LinearLayout) rootView
				.findViewById(R.id.nhan_xet_list);
		bai_dang_list = (LinearLayout) rootView
				.findViewById(R.id.bai_dang_list);

		bai_dang_button = (Button) rootView.findViewById(R.id.bai_dang);
		nhan_xet_button = (Button) rootView.findViewById(R.id.nhan_xet);
		bai_dang_button.setOnClickListener(onClickListener);
		nhan_xet_button.setOnClickListener(onClickListener);

		// Show info on UI
		showNguoiTao();
		showDiaDiem();
		showListBaiDang();
		showListNhanXet();

		list_image_url = dia_diem
				.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
		// Show List Image
		ArrayList<String> list_image_url_string = JsonUtils.getStringList(
				list_image_url, "url");
		for (int i = 0; i < list_image_url_string.size(); i++) {
			imageLoader.showImage(
					(ImageView) rootView.findViewById(list_id_image[i]),
					list_image_url_string.get(i));
		}

		return rootView;
	}

	private void showNguoiTao() {
		nguoi_tao.fetchIfNeededInBackground(new GetCallback<ParseUser>() {
			@Override
			public void done(ParseUser nguoidang, ParseException e) {
				nguoi_tao_value.setText(nguoidang
						.getString(QueriesConstant.TableUser.COLUMN_firstName)
						+ " "
						+ nguoidang
								.getString(QueriesConstant.TableUser.COLUMN_lastName));
				diem_tich_luy_value.setText(""
						+ nguoidang
								.getInt(QueriesConstant.TableUser.COLUMN_diem_tich_luy));
				cap_bac_value.setText(nguoidang
						.getString(QueriesConstant.TableUser.COLUMN_cap_bac));
				imageLoader.showImage(chi_tiet_bai_dang_avatar, nguoidang
						.getString(QueriesConstant.TableUser.COLUMN_photoURL));
			}
		});
	}

	private void showDiaDiem() {
		ten_dia_diem_value.setText(dia_diem
				.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem));
		luot_xem_value
				.setText(""
						+ dia_diem
								.getInt(QueriesConstant.TableDiaDiem.COLUMN_luot_xem));
		luot_visited_value
				.setText(""
						+ dia_diem
								.getInt(QueriesConstant.TableDiaDiem.COLUMN_luot_visited));
		diem_binh_chon_value
				.setText(""
						+ dia_diem
								.getDouble(QueriesConstant.TableDiaDiem.COLUMN_diem_binh_chon));
	}

	private void showListBaiDang() {
		diemSupport = new ChiTietDiaDiemSupport(getActivity(), bai_dang_list,
				nhan_xet_list, dia_diem,
				new CallBackFromParse<Object, Object>() {
					@Override
					public void done(Object request, Object result) {
						getNavigationController().navigateTo(
								BackActivity.class, false,
								Section.CHI_TIET_BAI_DANG, null);
					}
				});
		diemSupport.showListBaiDang();
	}

	private void showListNhanXet() {
		diemSupport.showListNhanXet();
	}

	@Override
	public void applyStyles() {
		super.applyStyles();
		getNavigationController().getNavigationBar().setActionTitle(
				getString(R.string.chi_tiet_dia_diem));

	}

	/**
	 * Xu ly khi get bai dang bi loi
	 */
	private void errorGetBaiDang() {
		Toaster.showToast(getActivity(), getString(R.string.thong_tin_bi_loi));
		getActivity().finish();
	}

	private OnClickListener onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.bai_dang:
				nhan_xet_button
						.setBackgroundResource(R.drawable.bg_button_click);
				bai_dang_button
						.setBackgroundResource(android.R.color.transparent);

				bai_dang_list.setVisibility(View.VISIBLE);
				nhan_xet_list.setVisibility(View.GONE);
				break;
			case R.id.nhan_xet:
				nhan_xet_button
						.setBackgroundResource(android.R.color.transparent);
				bai_dang_button
						.setBackgroundResource(R.drawable.bg_button_click);

				bai_dang_list.setVisibility(View.GONE);
				nhan_xet_list.setVisibility(View.VISIBLE);
				break;

			default:
				break;
			}
		}
	};
}